require 'net/http'
require 'net/https'
require 'uri'
require 'soap'
require 'environment'

include Soap

class SoapClient
	
	def initialize(body)
		@body = body
	end
	
	def run
		uri = URI.parse(Enviroment.configs['host'])		
		http = Net::HTTP.new(uri.host, uri.port)
		
		if uri.scheme == 'https'
			http.use_ssl = true
			http.verify_mode = OpenSSL::SSL::VERIFY_PEER
			http.ca_file = File.join(File.dirname(__FILE__), 'cacert.pem')
		end
	
		Soap::make(@body) do |soap|
			headers = {"Content-Type" => "text/xml"}
			response = http.post("/#{Enviroment.configs['context']}/soap/", soap, headers)
			yield response
		end
	end	

end

